package com.aiti.lulian.workflow;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.annotation.Transient;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@TableName("dj_task_history")
public class TaskHistory {
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    private String processInstanceId;

    private String taskDefinitionId;

    private String taskInstanceId;


    private ActionType action;

    private Date actionTime;

    private String actor;

    private String comment;

    private String rejectToNodeId;

    private String variablesJson;

    @TableField(exist = false)
    @JsonIgnore
    private Map<String, Object> variables;

    public TaskHistory() {
        this.actionTime = new Date();
        this.variables = new HashMap<>();
    }

    private String convertVariablesToJson() {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(variables);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("转换错误", e);
        }
    }

    private Map<String, Object> convertJsonToVariables(String json) {
        try {
            if (json == null || json.trim().isEmpty()) {
                return new HashMap<>();
            }
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
        } catch (Exception e) {
            throw new RuntimeException("转换错误", e);
        }
    }


    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getProcessInstanceId() { return processInstanceId; }
    public void setProcessInstanceId(String processInstanceId) { this.processInstanceId = processInstanceId; }

    public String getTaskDefinitionId() { return taskDefinitionId; }
    public void setTaskDefinitionId(String taskDefinitionId) { this.taskDefinitionId = taskDefinitionId; }

    public String getTaskInstanceId() { return taskInstanceId; }
    public void setTaskInstanceId(String taskInstanceId) { this.taskInstanceId = taskInstanceId; }

    public ActionType getAction() { return action; }
    public void setAction(ActionType action) { this.action = action; }

    public Date getActionTime() { return actionTime; }
    public void setActionTime(Date actionTime) { this.actionTime = actionTime; }

    public String getActor() { return actor; }
    public void setActor(String actor) { this.actor = actor; }

    public String getComment() { return comment; }
    public void setComment(String comment) { this.comment = comment; }

    public String getRejectToNodeId() { return rejectToNodeId; }
    public void setRejectToNodeId(String rejectToNodeId) { this.rejectToNodeId = rejectToNodeId; }

    public String getVariablesJson() { return variablesJson; }
    public void setVariablesJson(String variablesJson) {
        this.variablesJson = variablesJson;
        this.variables = convertJsonToVariables(variablesJson);
    }

    @JsonIgnore
    public Map<String, Object> getVariables() {
        if (variables == null && variablesJson != null) {
            variables = convertJsonToVariables(variablesJson);
        }
        return variables != null ? new HashMap<>(variables) : new HashMap<>();
    }

    public void setVariables(Map<String, Object> variables) {
        this.variables = variables != null ? new HashMap<>(variables) : new HashMap<>();
        this.variablesJson = convertVariablesToJson();
    }


}